package gui.detail;

import exceptions.AttributeAlreadyInRelationException;
import gui.detail.relation.AttributeWidget;

import javax.swing.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by:  Jason Ye
 * Date:        09/02/2012
 * Time:        18:01
 */
public class AttributePanel extends JPanel{

    Set<AttributeWidget> attributeWidgetSet = new HashSet<AttributeWidget>();
    
    Set<String> existingAttributeNames = new HashSet<String>();

    public AttributePanel() {

        BoxLayout thisLayout = new BoxLayout(this,BoxLayout.PAGE_AXIS);

        this.setLayout(thisLayout);
    }

    /**
     * adds an attribute to the set of attributes
     *
     * @param newAttributeName
     * @throws AttributeAlreadyInRelationException
     */
    public void addAttributeWidget(String newAttributeName, String newAttributeType, int newAttributeMaxSize) throws AttributeAlreadyInRelationException {
        
        if(existingAttributeNames.contains(newAttributeName)){

            AttributeWidget newAttribute = new AttributeWidget(newAttributeType,newAttributeName,newAttributeMaxSize);
            attributeWidgetSet.add(newAttribute);
            existingAttributeNames.add(newAttributeName);

            // add to contentPane
            this.add(newAttribute);
            this.revalidate();

        } else {
            throw new AttributeAlreadyInRelationException(newAttributeName);
        }
    }
    
    public void removeAttributeWidget(AttributeWidget newAttribute){

        this.remove(newAttribute);
        this.revalidate();
    }



}
